{{>licenseInfo}}

import http from "k6/http";
import { group, check, sleep } from "k6";

const BASE_URL = "{{baseURL}}";
// Sleep duration between successive requests.
// You might want to edit the value of this variable or remove calls to the sleep function on the script.
const SLEEP_DURATION = 0.1;
// Global variables should be initialized.
{{#extra}}
let {{{key}}} = "TODO_EDIT_THE_{{{value}}}";
{{/extra}}

export default function() {
    {{#requestGroups}}
    group("{{{groupName}}}", () => {
        {{#variables}}
        let {{{key}}} = "TODO_EDIT_THE_{{{value}}}";
        {{/variables}}
        {{#requests}}
        {{#-first}}
        let url = BASE_URL + `{{{path}}}{{=<% %>=}}<%#query%><%#-first%>?<%/-first%><%& key%>=<%& value%><%^-last%>&<%/-last%><%/query%><%={{ }}=%>`;
        // Request No. {{-index}}
        {{#body}}
        // TODO: edit the parameters of the request body.
        let body = {{#body}}{{=<% %>=}}{<%#parameters%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/parameters%>}<%={{ }}=%>{{/body}};
        {{/body}}
        {{#params}}
        let params = {{#params}}{{=<% %>=}}{headers: {<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%><%#responseType%>, "Accept": <%& responseType%><%/responseType%>}<%# auth%>, auth: "<%& auth%>"<%/auth%>}<%={{ }}=%>{{/params}};
        {{/params}}
        let request = http.{{method}}(url{{#body}}, body{{/body}}{{#params}}, params{{/params}});
        {{#k6Checks}}
        {{=<% %>=}}
        check(request, {
            "<%& description%>": (r) => r.status === <%& status%>
        });
        <%={{ }}=%>
        {{/k6Checks}}
        {{/-first}}
        {{^-first}}
        // Request No. {{-index}}
        {{#body}}
        // TODO: edit the parameters of the request body.
        body = {{#body}}{{=<% %>=}}{<%#parameters%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/parameters%>}<%={{ }}=%>{{/body}};
        {{/body}}
        {{#params}}
        params = {{#params}}{{=<% %>=}}{headers: {<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%>}<%# auth%>, auth: "<%& auth%>"<%/auth%>}<%={{ }}=%>{{/params}};
        {{/params}}
        request = http.{{method}}(url{{#body}}, body{{/body}}{{#params}}, params{{/params}});
        {{#k6Checks}}
        {{=<% %>=}}
        check(request, {
            "<%& description%>": (r) => r.status === <%& status%>
        });
        <%={{ }}=%>
        {{/k6Checks}}
        {{/-first}}
        sleep(SLEEP_DURATION);
        {{^-last}}

        {{/-last}}
        {{/requests}}
    });
    {{/requestGroups}}
}
